package com.poetry.jianjia.provider;

import com.poetry.jianjia.ResourceTable;
import com.poetry.jianjia.net.Article;
import com.poetry.jianjia.util.TextUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.RecycleItemProvider;
import ohos.agp.components.Text;
import ohos.app.Context;

/**
 * @author 裴云飞
 * @date 2021/1/26
 */

public class HomeArticleProvider extends RecycleItemProvider {

    private List<Article.Data.Datas> mDatas;
    private Context mContext;

    public HomeArticleProvider(Context context, List<Article.Data.Datas> datas) {
        mContext = context;
        mDatas = datas;
        if (mDatas == null) {
            mDatas = new ArrayList<>();
        }
    }

    @Override
    public int getCount() {
        return mDatas == null ? 0 : mDatas.size();
    }

    @Override
    public Object getItem(int position) {
        return Optional.of(mDatas.get(position));
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public Component getComponent(int position, Component componentP, ComponentContainer componentContainer) {
        Component component = componentP;
        ViewHolder viewHolder = null;
        if (component == null) {
            component = LayoutScatter.getInstance(mContext).parse(ResourceTable.Layout_item, null, false);
            if (viewHolder == null) {
                viewHolder = new ViewHolder(component);
                component.setTag(viewHolder);
            }
        } else {
            viewHolder = (ViewHolder) component.getTag();
        }
        Article.Data.Datas data = mDatas.get(position);
        if (viewHolder != null && data != null ) {
            if (!TextUtils.isEmpty(data.title)) {
                viewHolder.title.setText(data.title);
            }
            if (!TextUtils.isEmpty(data.author)) {
                viewHolder.author.setText(data.author);
            }
            if (!TextUtils.isEmpty(data.niceDate)) {
                viewHolder.time.setText(data.niceDate);
            }
            if (!TextUtils.isEmpty(data.superChapterName)) {
                viewHolder.category.setText(data.superChapterName);
            }
        }
        return component;
    }

    private static class ViewHolder {
        Text title;
        Text author;
        Text category;
        Text time;

        public ViewHolder(Component component) {
            title = (Text) component.findComponentById(ResourceTable.Id_title);
            title.setTruncationMode(Text.TruncationMode.ELLIPSIS_AT_END);
            author = (Text) component.findComponentById(ResourceTable.Id_author_name);
            category = (Text) component.findComponentById(ResourceTable.Id_category_name);
            time = (Text) component.findComponentById(ResourceTable.Id_time_name);
        }
    }
}